/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.agtranscontinental.sisref.sessionbean;

import com.agtranscontinental.dgs001.dto.PersonalDto;
import com.agtranscontinental.dgs001.model.Personal;
import com.agtranscontinental.sisref.sessionremote.PersonalEjbRemote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author ericson
 */
@Stateless
public class PersonalEjbBean implements PersonalEjbRemote {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext(unitName = "dgs001_pu")
    private EntityManager emDgs001;

    @Override
    public Personal getPersonal(String usuario, String contrasena) {
        Personal p = null;
        try {
            Query q = emDgs001.createNamedQuery("Personal.isUsuario");
            q.setParameter("usrEps", usuario);
            q.setParameter("clavEps", contrasena);
            p = (Personal) q.getSingleResult();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return p;
    }

    @Override
    public PersonalDto getPersonalDto(String usuario, String contrasena) {
        try {
            Query q = emDgs001.createNamedQuery("Personal.isUsuario");
            q.setParameter("usrEps", usuario);
            q.setParameter("clavEps", contrasena);
            Personal paso = (Personal) q.getSingleResult();
            if(paso!=null){
                return paso.toPersonalDto();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
